package repository

import (
	"context"
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/repository/dao"
)

type ConsumablesRepository struct {
	dao *dao.ConsumablesDAO
}

func NewConsumablesRepository(dao *dao.ConsumablesDAO) *ConsumablesRepository {
	return &ConsumablesRepository{
		dao: dao,
	}
}

func (r *ConsumablesRepository) Query(ctx context.Context, a domain.Consumable) ([]domain.Consumable, error) {
	Consumables, err := r.dao.Query(ctx, dao.Consumable{
		Name: a.Name,
	})

	if err != nil {
		return []domain.Consumable{}, err
	}

	returnValue := make([]domain.Consumable, 0)
	for _, v := range Consumables {
		returnValue = append(returnValue, domain.Consumable{
			Name:     v.Name,
			DictName: v.DictName,
			Stock:    v.Stock,
		})
	}

	return returnValue, nil
}

func (r *ConsumablesRepository) Select(ctx context.Context) ([]domain.Consumable, error) {
	consumables, err := r.dao.Select(ctx)

	if err != nil {
		return []domain.Consumable{}, err
	}

	returnValue := make([]domain.Consumable, 0)
	for _, v := range consumables {
		returnValue = append(returnValue, domain.Consumable{
			Id:       v.Id,
			Name:     v.Name,
			DictName: v.DictName,
		})
	}
	return returnValue, nil
}
